package net.zjitc.servlet;

import net.zjitc.domain.product;
import net.zjitc.service.ProductService;
import net.zjitc.service.impl.ProductServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/2/28 10:41
 */
@WebServlet(name = "FileAddServlet",urlPatterns = "/fileAdd")
public class FileAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        product product = new product();
        ProductService service = new ProductServiceImpl();
        //创建工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //创建解析器对象
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        try {
            //解析器解析
            List<FileItem> list = upload.parseRequest(request);
            //对应表单项
            for (FileItem item:list
                 ) {
                //是否为普通字段
                if (item.isFormField())
                {
                    if (item.getFieldName().equals("pname"))
                    {
                        product.setPname(item.getString("utf-8"));
                    }
                    if (item.getFieldName().equals("marketprice"))
                    {
                        product.setMarketprice(Double.parseDouble(item.getString()));
                    }
                    if (item.getFieldName().equals("shopprice"))
                    {
                        product.setShopprice(Double.parseDouble(item.getString()));
                    }
                    if (item.getFieldName().equals("ishot"))
                    {
                        product.setIshot(Integer.parseInt(item.getString()));
                    }
                    if (item.getFieldName().equals("pdesc"))
                    {
                        product.setPdesc(item.getString("utf-8"));
                    }
                }
                else {
                    //文件字段
                    String filename = item.getName();
                    //创建图片文件路径
                    String path = request.getServletContext().getRealPath("/products/5");
                    File file = new File(path);
                    //不存在时创建文件夹
                    if (!file.exists())
                    {
                        file.mkdirs();
                    }
                    //创建文件
                    File file1 = new File(path, filename);
                    if (!file1.exists())
                    {
                        file1.createNewFile();
                    }
                    //读取的文件流写入文件
                    item.write(file1);

                    /**
                     * 设置产品的图片属性
                     * 数据库中要存入的路径
                     */
                    product.setImage("/products/5/"+filename);
                }

            }
            service.add(product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/findAll").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
